< previous page page_257 next page >

Page 257
Declare Function cpuspeed Lib "cpuinf32.dll" (HiddenStruct As _
FREQ_INFO, ByVal counts As Long) As Long
The cpuspeed function sees the pointer to the FREQ_INFO structure and loads the structure with the result value before the function returns. The actual return value for the function is the address of the hidden structure, but since the structure passed as a HiddenStruct parameter will be loaded with the result data by the function, you won't need to use this address.
Wait, That's Not All . . .
If you call this function, you'll find it causes a Bad DLL calling convention error. This error occurs when the called function does not restore the stack pointer to its correct location.
But why would you get a calling convention error? Aren't the parameters correct?
They are. The problem is caused by the fact that the cpuspeed function uses the C calling convention. Normally, API calls use the stdcall (standard call) calling convention, in which the calling function pushes parameters onto the stack and the called parameter pops the parameters from the stack (thus restoring the stack pointer to the location it referenced before any parameters were pushed onto the stack). Under the C calling convention, it is the responsibility of the calling function to pop parameters from the stack. The cpuspeed function does not pop the two parameters off the stack as Visual Basic expects, so the Bad DLL calling convention error occurs.
But there are two subtle points to consider:
Visual Basic is smart enough to detect this situation and fix the stack pointer for you.
By the time the Bad DLL calling convention error is detected, the temporary structure has already been loaded with the resulting data.
In other words, it is perfectly safe to ignore this error!
The CPUInfo project demonstrates how to use a number of the functions in the cpuinf32.dll to obtain information about an Intel processor. The ShowSpeed function obtains the speed of the CPU using the cpuspeed function and adds the information to the list box, as shown here:
Private Sub ShowSpeed()
   Dim freq As FREQ_INFO
   Dim res&

 
< previous page page_257 next page >